import axios from 'axios';

// 创建axios实例
const api = axios.create({
  baseURL: '', // 空字符串，因为后端API直接以'/workflow'开头
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 请求拦截器
api.interceptors.request.use(
  config => {
    // 可以在这里添加token等认证信息
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    console.error('API请求错误:', error);
    return Promise.reject(error);
  }
);

// 工作流相关API
export const workflowService = {
  // 部署流程定义
  deployProcess: async (processData) => {
    return await api.post('/workflow/deploy', processData);
  },

  // 获取流程定义列表
  getProcessDefinitions: async () => {
    return await api.get('/workflow/process-definitions');
  },

  // 启动流程实例
  startProcess: async (processKey, variables = {}) => {
    return await api.post('/workflow/process-instances', {
      processKey,
      variables
    });
  },

  // 获取任务列表
  getTasks: async (assignee = null) => {
    const params = assignee ? { assignee } : {};
    return await api.get('/workflow/tasks', { params });
  },

  // 完成任务
  completeTask: async (taskId, variables = {}) => {
    return await api.post(`/workflow/tasks/${taskId}/complete`, { variables });
  },

  // 部署表单
  deployForm: async (formData) => {
    return await api.post('/workflow/forms/deploy', formData);
  },

  // 获取表单定义
  getFormDefinition: async (formKey) => {
    return await api.get(`/workflow/forms/${formKey}`);
  }
};

export default api;